package cc.minos.servers.openfire.filters
{
	import cc.minos.servers.filters.*;
	import cc.minos.servers.openfire.chats.ChatMessage;
	import cc.minos.servers.openfire.managers.OpenFireManager;
	import cc.minos.utils.SimpleDateFormatter;
	import flashx.textLayout.conversion.TextConverter;
	import flashx.textLayout.elements.DivElement;
	import flashx.textLayout.elements.FlowElement;
	import flashx.textLayout.elements.InlineGraphicElement;
	import flashx.textLayout.elements.LinkElement;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.TextFlow;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class MessageFilter
	{
		private static var filters:Array = [ Base64Filter , URLFilter , EmoticonFilter ];
		
		public static var selfColor:String = "407436"; //自己
		public static var youColor:String = "11406C"; //其他
		public static var systemColor:String = "D1494E"; //系统
		
		public static function changeToIM( message:ChatMessage ):FlowElement
		{
			var data:Date = message.time ? message.time : new Date();
			var time:String = SimpleDateFormatter.formatDate( data , "HH:mm:ss " );
			var nick:String = message.nick;
			var body:String = message.body;
			
			if ( !nick )
				return null;
			
			for each ( var filter:*in filters )
			{
				body = filter.decode( body );
			}
			
			var _color:String = nick == OpenFireManager.config.nickname ? selfColor : youColor;
			
			var msg:String = "" + color( _color , ( nick ) + "  " + time ) + " <br/>" + body;
			return getElement( msg );
		}
		
		public static function event( _event:String ):String
		{
			return "<a href=\"event:" + _event + "\">" + _event + "</a>"
		}
		
		public static function color( _color:String , str:String ):String
		{
			return "<font color=\"#" + _color + "\">" + str + "</font>";
		}
		
		private static function getElement( s:String ):FlowElement
		{
			var tf:TextFlow = TextConverter.importToFlow( s , TextConverter.TEXT_FIELD_HTML_FORMAT );
			if ( !tf )
				return null;
			var div:DivElement = new DivElement();
			var len:uint = tf.numChildren;
			for ( var i:uint = 0 ; i < len ; i++ )
			{
				var fe:FlowElement = div.addChild( tf.getChildAt( 0 ) );
			}
			return div;
		}
		
		public static function changeToSend( flow:TextFlow ):String
		{
			var body:String = "";
			
			//get body
			for ( var i:int = 0 , len:int = flow.numChildren ; i < len ; i++ )
			{
				var p:ParagraphElement = flow.getChildAt( i ) as ParagraphElement;
				if ( p )
				{
					for ( var j:int = 0 ; j < p.numChildren ; j++ )
					{
						var f:FlowElement = p.getChildAt( j );
						switch ( f.typeName )
						{
							case "span": 
								body += f.getText();
								break;
							case "img": 
								body += EmoticonFilter.encode( ( f as InlineGraphicElement ).source.toString() );
								//trace( ( f as InlineGraphicElement ).source.toString() );
								break;
							case "a": 
								body += " " + ( f as LinkElement ).href + " ";
								break;
							default: 
						}
					}
					if ( i < ( len - 1 ) )
						body += "<br/>";
				}
			}
			//trace( "send:  " + body );
			return Base64Filter.encode( body );
		}
	
	}

}